<?php

use Illuminate\Database\Eloquent\ModelNotFoundException;

class AdminProductController extends AdminController {

	public function getIndex(){
		$model = new Product;
		$data  = $model->getAllData();

		$this->layout->title = trans('product.list');
		$this->layout->content = View::make('admin.product.index', array('data' => $data));
	}

	public function getCreate(){
		if(!Cache::has('taxonomy-type-list-data')){
			$type = Taxonomy::whereType('type')->whereNull('deleted_at')->orderBy('order', 'desc')->lists('taxonomy', 'id');
			Cache::put('taxonomy-type-list-data', $type, 30);
		}

		$type = Cache::get('taxonomy-type-list-data');
		
		if(!Cache::has('taxonomy-brand-list-data')){
			$brand = Taxonomy::whereType('brand')->whereNull('deleted_at')->orderBy('order', 'desc')->lists('taxonomy', 'id');
			Cache::put('taxonomy-brand-list-data', $brand, 30);
		}

		$brand = Cache::get('taxonomy-brand-list-data');
		
		$this->layout->title = trans('product.create');
		$this->layout->content = View::make('admin.product.form', array('type' => $type, 'brand' => $brand));
	}

	public function getEdit($id){
		$this->layout->title = trans('product.edit');

		try{
			$model = new Product;
			$data = $model->findOrFail($id);

			if(!Cache::has('taxonomy-type-list-data')){
				$type = Taxonomy::whereType('type')->whereNull('deleted_at')->orderBy('order', 'desc')->lists('taxonomy', 'id');
				Cache::put('taxonomy-type-list-data', $type, 30);
			}

			$type = Cache::get('taxonomy-type-list-data');
			
			if(!Cache::has('taxonomy-brand-list-data')){
				$brand = Taxonomy::whereType('brand')->whereNull('deleted_at')->orderBy('order', 'desc')->lists('taxonomy', 'id');
				Cache::put('taxonomy-brand-list-data', $brand, 30);
			}

			$brand = Cache::get('taxonomy-brand-list-data');

			$this->layout->content = View::make('admin.product.form', array('product' => $data, 'type' => $type, 'brand' => $brand));
		}catch(ModelNotFoundException $e){
			App::abort(404);
		}
	}

	public function postSave(){
		$this->layout = null;

		$data = Input::all();
		
		$model = new Product;

		$validator = $model->validator($data);

		if($validator->fails()){
			if(isset($data['id']) && $data['id'] > 0){
				$url = 'admin/product/edit/' . $data['id'];
			}else{
				$url = 'admin/product/create/';
			}

			return Redirect::to($url)->withInput()->with('errors', $model->getErrorMessages());
		}else{
			if(Input::hasFile('image') && Input::file('image')->isValid()){
				$image = Input::file('image');
				$destinationPath = public_path() . '/upload/product';
				$name = date('Y-m-d_H-i-s') . '_' . $image->getClientOriginalName();
				$image->move($destinationPath, $name);
				$data['image'] = $name;
			}else{
				if(isset($data['old_image'])){
					$data['image'] = $data['old_image'];
				}else{
					$data['image'] = 'no-image.png';
				}			
			}

			$model->saveData($data);
			return Redirect::to('admin/product/');
		}
	}

	public function getDelete($id){
		$this->layout = null;

		try{
			$model = new Product;
			$data = $model->findOrFail($id);
			$data->delete();
			Cache::pull('product-all-data');
			
			return Redirect::to('admin/product/')->with('message', trans('product.deleted'));
		}catch(ModelNotFoundException $e){
			App::abort(404);
		}
	}
}
